package base.IO流;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class 解压流 {
    public static void main(String[] args) throws IOException {
        File src = new File("D:\\蒂法mod适应配件.zip");
        File dest = new File("C:\\Users\\TUO\\Desktop");
        Charset ch = Charset.forName("GBK");

        /*对于解压会自动遍历zip文件所有路径*/
        ZipInputStream zip = new ZipInputStream(new FileInputStream(src),ch);
        ZipEntry entry; //每个文件对象
        while ((entry = zip.getNextEntry()) != null){
            if (entry.isDirectory()){
                //是一个文件夹
                File file = new File(dest,entry.toString());//创建同样的文件夹
                file.mkdirs();
            }else {
                //是一个文件
                File file = new File(dest,entry.toString());
                FileOutputStream fos = new FileOutputStream(file);
                int len;
                byte[] bytes = new byte[1024 * 8];
                while ((len = zip.read(bytes)) != -1){  //read文件与gexNextEnrty相同顺序，为读完一个文件=-1
                    fos.write(bytes,0,len);
                }
                fos.close();
                zip.closeEntry();/*表示压缩包中的一个文件读取完毕了,关联（zip.getNextEntry()）*/
            }
        }
        zip.close();

    }
}
